#include "StdAfx.h"
#include "WinManager.h"
#include "Resource.h"
#include "Picture.h"

CWinManager::CWinManager(void)
{
}


CWinManager::~CWinManager(void)
{
}

CWinManager& CWinManager::getInstance()
{
	static CWinManager obj;
	return obj;
}

CWinManager* CWinManager::getInstancePtr()
{
	return &getInstance();
}

void CWinManager::Register(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CODESAFECLIENT));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= L"CodeSafeClient";
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	m_hInstance = hInstance;

	RegisterClassEx(&wcex);
}

LRESULT CALLBACK CWinManager::WndProc( HWND hWnd,UINT message, WPARAM wParam, LPARAM lParam )
{
	switch (message)
	{
	case WM_LBUTTONDOWN:
	case WM_LBUTTONUP:
	case WM_LBUTTONDBLCLK:
		{

		}
		break;
	case WM_PAINT:
		{
			CWinManager::getInstance().DrawPicture(hWnd);
		}
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

BOOL DrawBitmap (HDC hDC, INT x, INT y, HBITMAP hBitmap, DWORD dwROP)
{
	HDC       hDCBits;
	BITMAP    Bitmap;
	BOOL      bResult;

	if (!hDC || !hBitmap)
		return FALSE;

	hDCBits = CreateCompatibleDC(hDC);
	GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
	SelectObject(hDCBits, hBitmap);
	bResult = BitBlt(hDC, x, y, Bitmap.bmWidth, Bitmap.bmHeight, hDCBits, 0, 0, dwROP);
	DeleteDC(hDCBits);

	return bResult;
}

void CWinManager::DrawPicture( HWND hWnd )
{
	CPicture * lpBmp = FindPicture(hWnd);
	if (lpBmp == NULL)
	{
		ShowWindow(hWnd,SW_HIDE);
		return ;
	}

	ShowWindow(hWnd,SW_SHOW);

	HDC hDc = GetWindowDC(hWnd);
	DrawBitmap(hDc,0,0,lpBmp->GetHBITMAP(),SRCCOPY);
	ReleaseDC(hWnd,hDc);
}


CPicture * CWinManager::FindPicture(HWND hWnd)
{
	for (ItWin it = m_hWindows.begin(); it != m_hWindows.end(); ++it)
	{
		if ( it->hWnd == hWnd )
		{
			return it->lpBmp;
		}
	}
	return NULL;
}

CPicture * CWinManager::FindPicture(DWORD dwID)
{
	for (ItWin it = m_hWindows.begin(); it != m_hWindows.end(); ++it)
	{
		if ( it->dwID == dwID )
		{
			return it->lpBmp;
		}
	}
	return NULL;
}

void CWinManager::CreateWin( DWORD dwID,int x,int y,unsigned int nWidth,unsigned int nHeight ,DWORD dwStyle,DWORD dwParentID)
{
	HWND hWndParent = NULL;
	if ( dwParentID != 0 )
	{
		hWndParent = FindWin(dwParentID);
	}

	HWND hWnd = CreateWindow(L"CodeSafeClient", L"", dwStyle,
		x, y, nWidth, nHeight, hWndParent,NULL,m_hInstance, NULL);

	if (!hWnd)
	{
		return ;
	}

	ShowWindow(hWnd, SW_SHOW);
	WindowInfo t;
	t.dwID = dwID;
	t.hWnd = hWnd;
	t.lpBmp = NULL;

	m_hWindows.push_back(t);

	return ;
}

void CWinManager::On_CreateWindow( DWORD dwID,int x,int y,unsigned int nWidth,unsigned int nHeight,DWORD dwStyle,DWORD dwParentID  )
{
	CreateWin(dwID,x,y,nWidth,nHeight,dwStyle,dwParentID);
}

HWND CWinManager::FindWin( DWORD dwID )
{
	for (ItWin it = m_hWindows.begin(); it != m_hWindows.end(); ++it)
	{
		if ( it->dwID == dwID)
		{
			return it->hWnd;
		}
	}
	return NULL;
}
